const dialogPolyfill = require('dialog-polyfill');

function toast(msg) {
	let notification = document.getElementById('snack-bar');
	notification.MaterialSnackbar.showSnackbar(
		{
			message: msg
		}
	);
}

function actionToast(msg, btnText, btnHandler) {
	let notification = document.getElementById('snack-bar');
	notification.MaterialSnackbar.showSnackbar(
		{
			message: msg,
			actionHandler: btnHandler,
			actionText: btnText
		}
	);
}

function disableItems(arr) {
	for (let component of arr) {
		component.disabled = true;
	}
}

function enableItems(arr) {
	for (let component of arr) {
		component.disabled = false;
	}
}

function openDialog(dialog) {
	let dialogBox = document.getElementById(dialog);
	parcel.dialog = dialogBox;
	if (dialogBox) {
		dialogPolyfill.registerDialog(dialogBox);
		dialogBox.showModal();
	} else {
		console.error(`Dialog ${dialog} does not exists`);
	}
}

function closeDialog() {
	if (parcel.dialog) {
		parcel.dialog.close();
	}
}

function setTitle(title) {
	document.getElementById('title').innerText = title;
}

function bindEnterKey(dom, callback) {
	dom.addEventListener('keydown', (e) => {
		if (e.keyCode === 10 || e.keyCode === 13) {
			e.preventDefault();
			callback(dom);
		}
	});
}

function bindCtrlEnterKey(dom, callback) {
	dom.addEventListener('keydown', (e) => {
		if ((e.keyCode === 10 || e.keyCode === 13) && (e.ctrlKey || e.metaKey)) {
			e.preventDefault();
			callback(dom);
		}
	});
}
